package org.crystal.tradecraft.util;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Not ready for generic usage, just for interval strategy usage
 */
public class LimitedArrayList<E> extends ArrayList<E> {

    private final int limitedSize;

    public LimitedArrayList(int limitedSize) {
        super(limitedSize);
        this.limitedSize = limitedSize;
    }

    @Override
    public boolean add(E element) {
        if (size() + 1 > limitedSize) super.removeFirst();
        return super.add(element);
    }

    @Override
    public void add(int index, E element) {
        if (size() + 1 > limitedSize) super.removeFirst();
        super.add(index, element);
    }


    @Override
    public boolean addAll(Collection<? extends E> c) {
        if (size() + c.size() > limitedSize) {
            super.removeRange(0, c.size());
        }
        return super.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        if (size() + c.size() > limitedSize) {
            super.removeRange(0, c.size());
        }
        return super.addAll(index, c);
    }
}
